# Copyright 1999-2004 Gentoo Technologies, Inc.
# Distributed under the terms of the GNU General Public License v2
# $Header: $

inherit eutils

IUSE="usb gphoto2"

DESCRIPTION="Sane frontend and backend for Epson scanners"
HOMEPAGE="http://www.avasys.jp/english/linux_e/index.html"
SRC_URI="http://www.epkowa3.on.arena.ne.jp/iscan/v1131-1-gcc32/${P}-1.tar.gz"
SRC_URI="http://lx1.avasys.jp/iscan/v1140-3-gcc32/${P}-3.tar.gz"

SLOT="0"
LICENCE="GPL-2 epson-kowa"
KEYWORDS="~x86"

DEPEND="media-gfx/sane-backends
		usb? dev-libs/libusb"

src_unpack() {
	unpack ${A}
	epatch $FILESDIR/${P}-gcc-3.4.patch
}

src_compile() {
	local myconf
	myconf="$(use_enable usb libusb) $(use_with gphoto2)"
	./configure --prefix=/usr \
	--enable-debug \
	--sysconfdir=/etc \
	--mandir=/usr/share/man \
	--host=${CHOST} ${myconf} --with-gnu-ld --with-pic || die
	make || die

}

src_install() {
	#This prog overwrites files from sane-backend with older versions.
	#Delete them !
	#Also the doc goes in the wrong place
	einstall || die
	einfo ${WORKDIR}
	pushd ${WORKDIR}/../image/
	rm -f usr/lib/sane/libsane-epson.*
	rm -f usr/lib/sane/libsane-dll.*
	rm -f usr/lib/libsane.*
	rm -f etc/sane.d/epson.conf
	rm -f etc/sane.d/dll.conf
	rm -rf usr/doc
    
	# Now we work around wrong orderd linking (stdc+++ and dl)
	# Someone should recode this in a more clean way -:)
	# but thats just working:
	pushd usr/lib
	mkdir epkowa
	mv libesint23.so.1.0.0 epkowa/libesint23_orig.so.1.0.0
    mv libesint32.so.1.0.0 epkowa/libesint32_orig.so.1.0.0
	mv libesint1F.so.1.0.0 epkowa/libesint1F_orig.so.1.0.0
	mv libesmod.so.1.1.0 epkowa/libesmod_orig.so.1.1.0
	mv libe* epkowa/
	pushd epkowa
	gcc -shared -fPIC -lstdc++ -ldl ./libesint23_orig.so.1.0.0 -o libesint23.so.1.0.0
	gcc -shared -fPIC -lstdc++ -ldl ./libesint32_orig.so.1.0.0 -o libesint32.so.1.0.0
	gcc -shared -fPIC -lstdc++ -ldl ./libesint1F_orig.so.1.0.0 -o libesint1F.so.1.0.0
	gcc -shared -fPIC -lstdc++ -ldl ./libesmod_orig.so.1.1.0   -o libesmod.so.1.1.0
	popd; popd; popd
	dodoc ABOUT-NLS AUTHORS COPYING* README*
	insinto /etc/env.d
	doins $FILESDIR/99sane-epkowa
}


pkg_postinst() {
	einfo
	einfo "Hint: to enable the backend, add \"epkowa\" in \"/etc/sane.d/dll.conf\"       "
	einfo "Then edit \"/etc/sane.d/epkowa.conf\" to suite yuer needs.                    "
	einfo 
}
